package edu.hhu.analyse;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AnalyseHelp {
	public String getJson(String wellcode,String zhibiao1,String zhibiao2,String analysePra){

		String sql ="select "+zhibiao1+","+zhibiao2+" from gytrue where wellcode='"+wellcode+"'";

		//String sql1="SELECT tt.PWF,avg(tt.qq) QQ from  (SELECT t.qq ,t.PWF from gytrue t where t.wellcode ='1001090014') tt GROUP BY tt.PWF";
		
		List<Record> list = Db.use("mysql").find(sql);
		//System.out.println(list);
		double[] num=getMinMaxTotal(wellcode,zhibiao1,zhibiao2);
		
		String list1="[";
		double[] dbx=new double[(int)num[0]];
		double[] dby=new double[(int)num[0]];
		int i=0;
		for(Record r:list){
			double tempx=0;
			double tempy=0;

			String QQ="0";
			String PWF="0";
			if(r.getBigDecimal(zhibiao1)!=null){
				QQ=r.getBigDecimal(zhibiao1).toString();
				tempx=new Double(QQ);
			}
			if (r.getBigDecimal(zhibiao2) != null) {
				PWF=r.getBigDecimal(zhibiao2).toString();
				tempy=new Double(PWF);
			}
			dbx[i]=tempx;
			dby[i]=tempy;
//			System.out.println(i);
			//String QQ=r.getBigDecimal("QQ").toString();
			//String QL=r.getBigDecimal("QL").toString();
			list1=list1+"["+QQ+","+PWF+"],";
			i++;
		}
		list1=list1.substring(0,list1.length()-1)+"]";
		///////////////////
		/////////////////////////matlab拟合调用
		Analyse analse=new Analyse();
		String res=analse.analyseMethod(dbx, dby, new Integer(analysePra));
		res=res.substring(0, res.length()-1);
		
		double[] XXX=Xxx(num[1],num[2],800);
		double[] YYY=Yyy(XXX, res);
		String list2="[";
		
		for(int j=0;j<XXX.length;j++){
			list2=list2+"["+XXX[j]+","+YYY[j]+"],";
		}
		list2=list2.substring(0,list2.length()-1)+"]";
	
		String json="{\"well1\":"+list1+","+"\"well2\":"+list2+"}";
		return json;
	}
	
	//获取最大值最小值平均值
	public double[] getMinMaxTotal(String wellcode,String zhibiao1,String zhibiao2){
		String count ="select count(*) num,min("+zhibiao1+") minz,MAX("+zhibiao1+") maxz from gytrue where wellcode='"+wellcode+"'";

		List<Record> totalNum = Db.use("mysql").find(count);
		double[] num={0,0,0};
		double TotalNUM=0;
		double minz=0;
		double maxz=0;
		for(Record r:totalNum){
			TotalNUM=new Double(r.getNumber("num").toString());
			minz=new Double(r.getNumber("minz").toString());
			maxz=new Double(r.getNumber("maxz").toString());
		}
		num[0]=TotalNUM;
		num[1]=minz;
		num[2]=maxz;
		return num;
	}
	
	//获取拟合的横坐标
	public double[] Xxx(double min,double max,int countNum){
		double length=max-min;
		double[] result=new double[countNum]; 
		for(int i=0;i<countNum;i++){
			double temp=Math.random()*length+min;
			result[i]=temp;
		}
		return result;
	}
	
	//获取拟合的纵坐标
	public double[] Yyy(double[] xxx,String res){
		String[] temp=res.split(","); 
		double[] yyy=new double[xxx.length];
		double a=new Double(temp[0]);
		double b=new Double(temp[1]);
		for(int i=0;i<xxx.length;i++){
			yyy[i]=a*xxx[i]+b;
		}
		return yyy;
	}

}
